package wang.aweb.childcar.controller.management;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.model.pojo.Agent;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.AgentParam;
import wang.aweb.childcar.model.req.AddAgentReq;
import wang.aweb.childcar.model.req.UpdateAgentReq;
import wang.aweb.childcar.service.AgentService;

import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/management/agent/")
public class AgentController {
    @Autowired
    private AgentService agentService;

    @ApiOperation("获取全部代理商")
    @GetMapping("getAll")
    public ApiRestResponse getAll(){
        List<Agent> agents=agentService.getAll();
        return ApiRestResponse.success(agents);
    }
    @ApiOperation("获取全部数据")
    @PostMapping("lists")
    public ApiRestResponse getList(@RequestParam Integer page, @RequestParam Integer limit, @RequestBody AgentParam agentParam){
        PageInfo pageInfo=agentService.getList(page,limit,agentParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }

    @ApiOperation("添加代理商")
    @PostMapping("add")
    public ApiRestResponse add(@Valid @RequestBody AddAgentReq addAgentReq) throws NoSuchAlgorithmException {
        agentService.create(addAgentReq);
        return ApiRestResponse.success();
    }
    @ApiOperation("设置代理商信息")
    @PostMapping("update")
    public ApiRestResponse update(@Valid @RequestBody UpdateAgentReq updateAgentReq){
        Agent agent=new Agent();
        BeanUtils.copyProperties(updateAgentReq,agent);
        agentService.update(agent);
        return ApiRestResponse.success();
    }
    @ApiOperation("设置可用或不可用")
    @PostMapping("disableorEnable")
    public ApiRestResponse disableOrEnable(@RequestBody Map<String,Integer> reqMap){
        agentService.disableOrEnable(reqMap.get("id"));
        return ApiRestResponse.success();
    }
    @ApiOperation("删除代理商")
    @PostMapping("delete")
    public ApiRestResponse delete(@RequestBody Map<String,Integer> reqMap){
        agentService.deleteAgentById(reqMap.get("id"));
        return ApiRestResponse.success();
    }
    @ApiOperation("代理商结算")
    @PostMapping("jiesuan")
    public ApiRestResponse jiesuan(@RequestBody Map<String,String> reqMap){
        Integer id=Integer.valueOf(reqMap.get("id"));
        Float money=Float.valueOf(reqMap.get("money"));
        agentService.jiesuan(id,money);
        return ApiRestResponse.success();
    }
}
